package breeze.pixel.weather.service.app_widget.configures;

import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.load.MultiTransformation;
import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.Resource;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.google.android.material.slider.Slider;
import com.yalantis.ucrop.UCrop;
import com.yanzhenjie.album.Album;
import com.yanzhenjie.album.AlbumFile;

import java.io.File;
import java.security.MessageDigest;
import java.text.MessageFormat;

import breeze.pixel.weather.App;
import breeze.pixel.weather.BaseView;
import breeze.pixel.weather.R;
import breeze.pixel.weather.apps_util.transformation.AppBrightnessTransformation;
import breeze.pixel.weather.apps_util.utils.AppToolUtil;
import jp.wasabeef.glide.transformations.BlurTransformation;
import jp.wasabeef.glide.transformations.gpu.BrightnessFilterTransformation;
import top.defaults.colorpicker.ColorPickerView;

public abstract class BaseWidgetConfigActivity extends BaseView {

    protected final int WIDGET_MODE_1 = 1;
    protected final int WIDGET_MODE_2 = 2;

    private int scaleWidth, scaleHeight;

    private ImageView btnSave;
    private FrameLayout frameLayout;
    private TextView transparencyText;
    private Slider transparencySlider;
    private Slider brightnessSlider;
    private ImageView imgBg;
    private Button btnSelectFile;
    private ColorPickerView colorPickerView;
    private LinearLayout colorPreview;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setStatusBarColor(Color.WHITE);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        setContentView(R.layout.activity_config_widget);

        colorPickerView = find(R.id.colorPickerView);
        colorPreview = find(R.id.linearlayout);
        colorPickerView.setInitialColor(Color.BLACK);
        colorPickerView.subscribe((color, fromUser, shouldPropagate) -> {
            colorPreview.setBackgroundColor(color);
            onTextColorChanged(color);
        });

        btnSave = findViewById(R.id.imageView11);
        btnSave.setOnClickListener(v -> onSave());

        frameLayout = findViewById(R.id.framelayout);
        frameLayout.addView(returnChildView());

        transparencyText = findViewById(R.id.textView9);
        transparencySlider = findViewById(R.id.slider);
        transparencySlider.setValueFrom(0);
        transparencySlider.setValueTo(255);
        transparencySlider.setStepSize(1);
        transparencySlider.setValue(255);
        transparencySlider.addOnChangeListener((slider, value, fromUser) -> {
            onTransparentChanged((int) value);
            transparencyText.setText(MessageFormat.format("{0}/255", (int) value));
        });

        imgBg = findViewById(R.id.imageView10);
        btnSelectFile = findViewById(R.id.button2);
        btnSelectFile.setOnClickListener(v -> {
            Album.album(BaseWidgetConfigActivity.this)
                    .singleChoice()
                    .onResult(result -> {
                        if (result.size() > 0) {
                            AlbumFile albumFile = result.get(0);
                            String tempPicFile = AppToolUtil.getTempPicFile(App.getContext());
                            UCrop.of(Uri.fromFile(new File(albumFile.getPath()))
                                    ,Uri.fromFile(new File(tempPicFile)))
                                    .withAspectRatio(scaleWidth, scaleHeight)
                                    .start(this,100);
                        }
                    })
                    .start();
        });

        brightnessSlider = findViewById(R.id.slider2);
        brightnessSlider.setValueFrom(-100);
        brightnessSlider.setValueTo(100);
        brightnessSlider.setStepSize(2);
        brightnessSlider.addOnChangeListener((slider, value, fromUser) -> onBrightnessChanged((int) value));

        int widgetMode = getWidgetMode();
        if (widgetMode == WIDGET_MODE_1) {
            scaleWidth = 600;
            scaleHeight = 300;
        } else if (widgetMode == WIDGET_MODE_2) {
            scaleHeight = scaleWidth = 400;
        }
    }

    /**
     * 获取控件ID
     *
     * @return
     */
    protected int getWidgetID() {
        Intent intent = getIntent();
        return intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
    }

    protected abstract int getWidgetMode();

    protected abstract View returnChildView();

    protected abstract void onPictureSelected(File file);

    protected abstract void onTransparentChanged(int val);

    protected abstract void onBrightnessChanged(int val);

    protected abstract void onTextColorChanged(int val);

    protected abstract void onSave();

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100 && resultCode == RESULT_OK) {
            String tempPicFile = AppToolUtil.getTempPicFile(App.getContext());
            onPictureSelected(new File(tempPicFile));
            brightnessSlider.setEnabled(true);
            Glide.with(getApplicationContext()).load(tempPicFile).into(imgBg);
        }
    }
}
